<?php


namespace app\controller\archives;
require_once __DIR__.'/../../common/AuthController.php';
use app\common\AuthController;


/**
 * 商圈
 */
class MerchantCircle extends AuthController
{   


    /**
     * 初始化操作
     */
    public function init()
    {

    }
	

    /**
     * 获取商圈列表
     */
	public function api_archives_merchantCircle_getList($msg)
    {   
        global $db;
        $info = [
            'page' =>  isset($msg['data']['page'])?$msg['data']['page']:1,
            'rows' => isset($msg['data']['rows'])?$msg['data']['rows']:15,
            'id' => isset($msg['data']['id'])?$msg['data']['id']:'',
            'name' => isset($msg['data']['name'])?$msg['data']['name']:'',
            'region_name' => isset($msg['data']['region_name'])?$msg['data']['region_name']:'',
        ];
        $limit = ($info['page']-1)*$info['rows'].','.$info['rows'];
        $where = "is_delete = 0";
        if(!empty($info['id'])){
            $where .= " AND id =".$info['id'];
        }
        if(!empty($info['name'])){
            $where .= " AND name LIKE '%".$info['name']."%'";
        }
        if(!empty($info['region_name'])){
            $where .= " AND region_name LIKE '%".$info['region_name']."%'";
        }
        $list = $db->query("select * from merchant_circle where ".$where ." order by id  desc limit ".$limit); 
        $count =  $db->query("SELECT count(*) as count from merchant_circle WHERE ".$where);
        return $this->outMessage(10000,'获取成功',$list,'success',$count[0]['count']);
	}



    /**
     * 添加
     */
    public function api_archives_merchantCircle_add($msg)
    {   
        $info = [
            'name' =>  isset($msg['data']['name'])?$msg['data']['name']:'',
            'region_id' => isset($msg['data']['region_id'])?$msg['data']['region_id']:'',
            'region_name' => isset($msg['data']['region_name'])?$msg['data']['region_name']:''
        ];
        foreach ($info as $v) {
            if(empty($v)){
                return $this->outMessage(-1,'参数不能为空');
            }
        }
        $info['remark'] = isset($msg['data']['remark'])?$msg['data']['remark']:'';
        if(!$this->validate->checkName($info['name'])){
            return $this->outMessage(-1,'请输入正确的名称');
        }
        if(!empty($info['remark'])){
            if(!$this->validate->checkName($info['intro'],2,100)){
                return $this->outMessage(-1,'请输入正确的备注');
            }
        }
        
        $res = $this->table->find('merchant_circle',['name'=>$info['name'],"region_id"=>$info['region_id']]);
        if($res){
            return $this->outMessage(-1,'该地区名称已存在');
        }
        $data = [
            'name' => $info['name'],
            'region_id' => $info['region_id'],
            'region_name' => $info['region_name'],
            'admin_id' => $this->admin_id,
            'create_time' => date('Y-m-d H:i:s',time())
        ];
        $res = $this->table->insert('merchant_circle',$data);
        if(!$res){
            return $this->outMessage(-1,'添加失败');
        }
        return $this->outMessage(10000,'添加成功');
    }



    /**
     * 删除
     */
    public function api_archives_merchantCircle_delete($msg)
    {
        $id = isset($msg['data']['id'])?$msg['data']['id']:'';
        if(empty($id)){
            return $this->outMessage(-1,'ID不能为空');
        }
        $res = $this->table->delete('merchant_circle',['id'=>$id]);
        if(!$res){
            return $this->outMessage(-1,'删除失败');
        }
        return $this->outMessage(10000,'删除成功');
    }



    /**
     * 审核
     */
    public function api_archives_merchantCircle_check($msg)
    {
        $info = [
            'id' =>  isset($msg['data']['id'])?$msg['data']['id']:'',
            'status' => isset($msg['data']['status'])?$msg['data']['status']:'',
        ];
        foreach ($info as $v) {
            if(empty($v)){
                return $this->outMessage(-1,'参数不能为空');
            }
        }
        $info['check_remark'] = isset($msg['data']['check_remark'])?$msg['data']['check_remark']:'';
        if(!empty($info['check_remark'])){
            if(!$this->validate->checkName($info['check_remark'],2,100)){
                return $this->outMessage(-1,'请输入正确的审核备注');
            }
        }
        $circle = $this->table->find('merchant_circle',['id'=>$info['id']]);
        if(empty($circle)){
            return $this->outMessage(-1,'该商圈不存在');
        }
        if($circle['status'] == $info['status']){
            return $this->outMessage(-1,'不可重复审核');
        }
        $data = [
            'status' => $info['status'],
            'check_admin' => $_SESSION['admin_id'],
            'check_time' => date('Y-m-d H:i:s',time()),
            'check_remark' => $info['check_remark'],
            'update_time' => date('Y-m-d H:i:s',time()),
        ];
        $res = $this->table->update('merchant_circle',['id'=>$info['id']],$data);
        if(!$res){
            return $this->outMessage(-1,'审核失败');
        }
        return $this->outMessage(10000,'审核成功');
    }

 

}
